<?php
class Customer extends BaseDB
{
	function __construct()
	{
		$this->table = 'customers';
		parent::__construct();
	}
	
	/**
	 * 添加客户信息
	 *
	 * @param $data array('customer_index', 'customer_index_pid', 'name', 'area', 'address', 'zip_code', 'contact', 'tel', 'email')
	 */
	public function add($data)
	{
		if (empty($data['customer_index']) || !is_numeric($data['customer_index']) || $data['customer_index'] < 0 || 
			empty($data['customer_index_pid']) || !is_numeric($data['customer_index_pid']) || $data['customer_index_pid'] < 0) {
			return false;
		}
		
		if (
			empty($data['name']) ||
			empty($data['area']) || 
			empty($data['address']) || 
			empty($data['zip_code']) || 
			empty($data['contact']) || 
			empty($data['tel'])) {
			return false;
		}
		
		return $this->insert($data);
	}
	
	public function getInfoById($id)
	{
		if (empty($id))return false;
		return $this->select(array('id'=>$id));
	}
	
	public function getInfoByIds($ids)
	{
		if (empty($ids) || !is_array($ids))return false;
		return $this->getXByIds($ids);
	}
	
	public function getList($start = 0 , $count = 20)
	{
		$start = max(0, (int)$start);
		$count = max(20, (int)$count);
		return $this->select(false, $start, $count);
	}
	
	public function queryByName($name, $start = 0, $count = 1)
	{
		if (empty($name)) return false;
		$start = max(0, (int)$start);
		$count = max(0, (int)$count);
		$sql = "select * from " . $this->table . " where name like '%$name%' limit $start, $count order by id desc";
		return $this->db->getAll($sql, array());
		//$sql = "select * from " . $this->table . " where MATCH(name) AGAINST('{$name}' IN BOOLEAN MODE) limit $start, $count order by id desc ";
		//return $this->db->getAll($sql);
	}
	
	public function getInfoByIndex($index) {
		if (empty($index)) {
			return false;
		}
		
		return $this->select(array('customer_index'=>$index), 0, 1);
	}
	
	/**
	 * 修改信息
	 *
	 * @param array $data
	 * @param array $where
	 * @return 影响行数；如没变化，不影响任何记录返回0
	 */
	public function modify($data, $where) {
		if (empty($data) || empty($where)) {
			return false;
		}
		
		return $this->update($data, $where);
	}
}
?>